﻿//=============================================================
// Copyright (C) 2011 深圳市中旭企业管理股份有限公司
// 作 者：曾洋
// 日 期：2011/10/27
// 描 述: 部门业务处理类
// 版 本：1.00
// 修改历史纪录
// 版 本  修改时间      修改人            修改内容
// 1.00   2011/11/1    曾洋           
//=============================================================

using System;
using System.Collections.Generic;
using System.Linq;
using ZX.BLL.Extend;
using ZX.SYS.Library;
using ZX.DBModule;
using ZX.EHR.Library;
using ZX.Common;

namespace ZX.BLL
{
    public partial class BDepartment : BLL.Extend.BaseBussnies
    {
        public BDepartment()
        {
            this.entity = new DatDepartment();
        }
        /// <summary>
        /// 获取该部门下所有子部门信息
        /// </summary>
        /// <param name="deptPath">部门路径</param>
        /// <returns>IList：DatDepartment</returns>
        public IList<DatDepartment> GetDeptListByID(string deptPath)
        {
            return this.GetEntitys<DatDepartment>("DeptPath LIKE '" + deptPath + "%'");
        }

        /// <summary>
        /// 创建TreeCode
        /// </summary>
        /// <param name="parentID"></param>
        /// <returns></returns>
        public string CreateNewTreeCode(Guid parentID)
        {
            DatDepartment pDept= this.GetEntityById(parentID.ToString()) as DatDepartment;
            IList<DatDepartment> deptList = this.GetEntitys<DatDepartment>("ParentID='" + parentID + "'");
            if (deptList == null)
            {
                return pDept.DeptPath + "-001";
            }
            else
            {
                string bDept = deptList.Max(p => p.DeptPath);
                if (bDept == string.Empty || bDept.IndexOf("-") == -1)
                {
                    return pDept.DeptPath + "-001";
                }
                 string[] bDepts = bDept.Split('-');
                return pDept.DeptPath + "-" + bDepts[bDepts.Length - 1].Replace(System.Int32.Parse(bDepts[bDepts.Length - 1]).ToString().Replace('@', ' '), "") + (System.Int32.Parse(bDepts[bDepts.Length - 1]) + 1);
            }
               
        }


        /// <summary>
        /// 返回树型结构
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="Condition"></param>
        /// <returns></returns>
        public override IList<T> GetEntitys<T>(string Condition)
        {
            IList<T> list = base.GetEntitys<T>(Condition);
            IList<DatDepartment> List = new List<DatDepartment>();
            foreach (var en in list)
            {
                DatDepartment _en = en as DatDepartment;
                List.Add(_en);
            }
            IList<DatDepartment> _list = new List<DatDepartment>();
            FillList(Guid.Empty, List, _list);
            IList<T> _mlist = new List<T>();
            foreach (var en in _list)
            {
                //object o = en;
                string json = en.ToJSON();
                T t = Common.Globals.JSonToObject<T>(json); //System.Converter;
                _mlist.Add(t);
                //T t = default(T);// (T)this.entity;
            }
            //return _list.To(List(); 
            return _mlist;
        }

        public override IList<BaseEntity> GetEntitysSplitPage(string Condition)
        {
            //return base.GetEntitysSplitPage(Condition);
            return GetEntitysByCondition(Condition);
        }
        public override IList<BaseEntity> GetEntitysByCondition(string Condition)
        {
            IList<DatDepartment> list = this.GetEntitys<DatDepartment>(Condition);
            IList<DatDepartment> _nlist = new List<DatDepartment>();
            FillList(Guid.Empty, list, _nlist);
            IList<BaseEntity> _list = new List<BaseEntity>();
            foreach (DatDepartment entity in _nlist)
            {
                _list.Add(entity);
            }
            return _list;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="parentId"></param>
        /// <param name="List"></param>
        /// <param name="mList"></param>
        private void FillList(Guid parentId, IList<DatDepartment> List, IList<DatDepartment> mList)
        {
            foreach (var en in List.Where(p => p.ParentID == parentId))
            {
                if (en.ParentID != Guid.Empty)
                {
                    var _p = List.Where(p => p.DeptID == en.ParentID).First();
                    en.Levelno = _p.Levelno + 1;
                    string str = "";

                    for (int i = 0; i < en.Levelno; i++)
                    {
                        str = str + ". ";
                    }
                    en.DeptName = str + en.DeptName;
                }
                else
                {
                    en.Levelno = 0;
                }
                mList.Add(en);
                FillList(en.DeptID, List, mList);
            }
        }
    }
}
